'use strict'
const moment = require('moment');
/**
 * 日志记录中间件
 */
module.exports = async (ctx, next) => {
  const { log } = ctx.service
  const { request, req, app } = ctx
  if (app.config.env === 'production') { // 生产环境记录
    if (request.path !== '/api/v1/user/login') {
      const res = app.verifyToken(ctx)
      // console.log('==============================')
      ctx.logger.info(ctx.request)
      log.addLog({
        admin: res.username,
        ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
        time: moment().format('YYYY-MM-DD HH:mm:ss'),
        comment: request.method + ' ' + request.path
      })
    }
  }
  return next()
}
